package com.app.service.impl;

import com.app.common.Result;
import com.app.mapper.ArticleMapper;
import com.app.model.Article;
import com.app.service.IArticleService;
import com.app.vo.AdminRoleVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yoominic
 * @since 2024-06-20
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements IArticleService {
    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public Result<List<Article>> getMiniArticles(String code) {
        List<Article> articles = articleMapper.selectList(new QueryWrapper<Article>()
                .eq("code", code));
        return Result.success(articles);
    }

    @Override
    public Result<Object> getAdminArticles(Integer page, Integer limit) {
        Page<Article> pageObj = new Page(page, limit);
        Page<Article> articlePage = articleMapper.selectPage(pageObj, null);
        AdminRoleVo adminRoleVo = new AdminRoleVo();
        adminRoleVo.setArticleList(articlePage.getRecords());
        adminRoleVo.setCount((int) articlePage.getTotal());
        return Result.success(adminRoleVo);
    }

    @Override
    public Result<Object> updateArticle(Article article) {
        article.setUpdateTime(new Date());
        if (articleMapper.updateById(article) > 0) {
            return Result.success(200);
        } else {
            return Result.error(400, "更新失败");
        }
    }
}
